﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Text;

namespace NewstorePlateformeDAO
{
    public static class EntityObjectExtension
    {
        /// <summary>
        /// Ajoute l'objet aux données
        /// </summary>
        /// <param name="entityObject">The entity object.</param>
        /// <param name="context">The context.</param>
        public static int Add(this EntityObject entityObject, Newstore_PL_GOLD_BDDEntities context)
        {
            int nb = 0;

            try
            {
                // Ajout de l'objet dans le contexte de données
                context.AddObject(entityObject.GetType().Name, entityObject);

                // Persistance du contexte de données
                nb = context.SaveChanges();
            }
            catch (Exception ex)
            {
                EntitiesHelper.DisplayDebug("EntityObjectExtension.Add", ex);
            }

            return nb;
        }

        /// <summary>
        /// Edits the specified entity object.
        /// </summary>
        /// <param name="entityObject">The entity object.</param>
        /// <param name="context">The context.</param>
        public static int Edit(this EntityObject entityObject, Newstore_PL_GOLD_BDDEntities context)
        {
            int result = 0;

            try
            {
                try
                {
                    // Try to save changes, which may cause a conflict.
                    result = context.SaveChanges();
                }
                catch (OptimisticConcurrencyException)
                {
                    // Resolve the concurrency conflict by refreshing the 
                    // object context before re-saving changes. 
                    context.Refresh(System.Data.Objects.RefreshMode.ClientWins, entityObject);

                    // Save changes.
                    result = context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                EntitiesHelper.DisplayDebug("EntityObjectExtension.Edit", ex);
            }

            return result;
        }

        /// <summary>
        /// Deletes the specified entity object.
        /// </summary>
        /// <param name="entityObject">The entity object.</param>
        /// <param name="context">The context.</param>
        public static int Delete(this EntityObject entityObject, Newstore_PL_GOLD_BDDEntities context)
        {
            int result = 0;

            try
            {
                // Modification du contexte de données
                context.DeleteObject(entityObject);

                // Persistance du contexte de données
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                EntitiesHelper.DisplayDebug("EntityObjectExtension.Delete", ex);
            }

            return result;
        }

        /// <summary>
        /// Refreshes the specified entity object.
        /// </summary>
        /// <param name="entityObject">The entity object.</param>
        public static void Refresh(this EntityObject entityObject, Newstore_PL_GOLD_BDDEntities context)
        {
            try
            {
                // raffraichissement de l'objet dans le contexte de données
                context.Refresh(System.Data.Objects.RefreshMode.StoreWins, entityObject);
            }
            catch (Exception ex)
            {
                EntitiesHelper.DisplayDebug("EntityObjectExtension.Refresh", ex);
            }
        }
    }
}
